package com.syt.dome.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.syt.dome.repository.DepartmentRepository;
import com.syt.dome.service.DepartmentService;
import com.syt.entity.model.hosp.Department;
import com.syt.entity.model.hosp.Schedule;
import com.syt.entity.vo.hosp.DepartmentQueryVo;
import com.syt.entity.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;




// * 获取部门
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }




    //根据科室编号，和医院编号，查询科室名称
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department != null) {
            return department.getDepname();
        }
        return null;
    }




    // 根据科室编号查询科室信息
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {

        // 创建 list 集合，用于最终数据的封装
        List<DepartmentVo> result=new ArrayList<>();

        // 根据医院编号，查询医院所有科室信息
        // 首先 加入 Department 用于创建对象设置值
        Department department=new Department();
        // 设置值
        department.setHoscode(hoscode);
        // 加入设置的值
        Example departmentExample = Example.of(department);// 用于封装条件
        // 所有科室列表 departmentList
        List<Department> departmentList = departmentRepository.findAll(departmentExample);

        // 根据打科室进行分组 通过 bigcode进行分组  获取每个大科室里面下级子科室
             // 通过 java8 数据流来实现分组效果
        Map<String, List<Department>> departmentMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));

        //遍历map集合 deparmentMap
        for (Map.Entry<String,List<Department>> entry: departmentMap.entrySet()) {
            //大科室编号
            String bigcode = entry.getKey();
            //大科室编号对应的全局数据
            List<Department> deparment1List = entry.getValue();

            // 封装大科室
             DepartmentVo departmentVo1=new DepartmentVo();
             departmentVo1.setDepcode(bigcode);// 打科室的编号
            departmentVo1.setDepname(deparment1List.get(0).getBigname());

            // 封装小科室
            List<DepartmentVo> children=new ArrayList<>();// 封装子节点

            for(Department department1 : deparment1List){

                DepartmentVo departmentVo2=new DepartmentVo();
                departmentVo2.setDepcode(department1.getDepcode());
                departmentVo2.setDepname(department1.getDepname());


                //封装到list集合
                children.add(departmentVo2);

            }

            //把小科室list集合放到大科室children里面
            departmentVo1.setChildren(children);
            //放到最终result里面
            result.add(departmentVo1);

        }

        //返回
        return result;

    }








    //根据医院编号，查询医院所有科室列表 副本
    @Override
    public List<DepartmentVo> findDeptTrees(String hoscode) {
        //创建list集合，用于最终数据封装
        List<DepartmentVo> result = new ArrayList<>();

        //根据医院编号，查询医院所有科室信息
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example example = Example.of(departmentQuery);
        //所有科室列表 departmentList
        List<Department> departmentList = departmentRepository.findAll(example);

        //根据大科室编号  bigcode 分组，获取每个大科室里面下级子科室
        Map<String, List<Department>> deparmentMap =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //遍历map集合 deparmentMap
        for(Map.Entry<String,List<Department>> entry : deparmentMap.entrySet()) {
            //大科室编号
            String bigcode = entry.getKey();
            //大科室编号对应的全局数据
            List<Department> deparment1List = entry.getValue();
            //封装大科室
            DepartmentVo departmentVo1 = new DepartmentVo();
            departmentVo1.setDepcode(bigcode);
            departmentVo1.setDepname(deparment1List.get(0).getBigname());

            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for(Department department: deparment1List) {
                DepartmentVo departmentVo2 =  new DepartmentVo();
                departmentVo2.setDepcode(department.getDepcode());
                departmentVo2.setDepname(department.getDepname());
                //封装到list集合
                children.add(departmentVo2);
            }
            //把小科室list集合放到大科室children里面
            departmentVo1.setChildren(children);
            //放到最终result里面
            result.add(departmentVo1);
        }
        //返回
        return result;
    }










    // 删除科室接口
    @Override
    public void remove(String hoscode, String depcode) {

        // 根据医院编号 和 科室编号查询
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (null != department) {
            // 调用删除方法
            //departmentRepository.delete(department);
            departmentRepository.deleteById(department.getId());
        }


    }




    // 查看科室信息的接口
    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {

        // 创建配置 Pageable 对象，设置当前页和每页的记录数
        //0 是第一页 所有 page-1
        Pageable pageable = PageRequest.of(page - 1, limit);
        // 创建 Example 对象
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);

        // 设置一个值
        department.setIsDeleted(0);// 删除的标志

        ExampleMatcher resultMatcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);

        Example<Department> example = Example.of(department, resultMatcher);

        Page<Department> departments = departmentRepository.findAll(example, pageable);
        return departments;
    }


    // 上传科室信息
    @Override
    public void save(Map<String, Object> stringObjectMap) {


        // stringObjectMap 转换成 department 对象

        String toring = JSONObject.toJSONString(stringObjectMap);
// 转换
        Department department = JSONObject.parseObject(toring, Department.class);


        // 判断是否存在科室信息
        // 根据医院编号 和 科室编号查询
        Department targetDepartment = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());

        // 判断
        if (targetDepartment != null) {
//copy不为null的值，该方法为自定义方法
            targetDepartment.setUpdateTime(new Date());
            targetDepartment.setIsDeleted(0);
            departmentRepository.save(targetDepartment);
        } else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
            System.out.println("++++++++++++++++" + department);
        }
    }


//    //上传科室接口
//    @Override
//    public void save(Map<String, Object> paramMap) {
//        //paramMap 转换department对象
//        String paramMapString = JSONObject.toJSONString(paramMap);
//        Department department = JSONObject.parseObject(paramMapString,Department.class);
//
//        //根据医院编号 和 科室编号查询
//        Department departmentExist = departmentRepository.
//                getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
//        //判断
//        if(departmentExist!=null) {
//            departmentExist.setUpdateTime(new Date());
//            departmentExist.setIsDeleted(0);
//            departmentRepository.save(departmentExist);
//        } else {
//            department.setCreateTime(new Date());
//            department.setUpdateTime(new Date());
//            department.setIsDeleted(0);
//            departmentRepository.save(department);
//        }
//    }


}
